package dao.impl;

import dao.BuyDao;
import entity.BuyList;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import utils.DruidUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class BuyDaoImpl implements BuyDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtils.getDataSource());
    @Override
    public List<BuyList> selectBuyLists() {
        String sql = "select* from supplier_commodity";
        List<BuyList> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(BuyList.class));
        return query;
    }

    @Override
    public void updateBuyStatus(int scid) {
        String sql = "update supplier_commodity set SCstatus = 1 where SCid = ?";
        jdbcTemplate.update(sql,scid);
    }

    @Override
    public void addBuyList(int cid, int sum) {
        String sql = " insert into supplier_commodity(cid,scid,sum,scstatus,createtime) values (?,?,?,?,?)";
        Integer scid = UUID.randomUUID().toString().hashCode();
        scid = scid < 0 ? - scid : scid;
        jdbcTemplate.update(sql,cid,scid,sum,0,new Date());
    }
}
